﻿using fastJSON;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WCFMINERP.COM;

namespace WCFMINERP.Hosting
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
                {
                    MessageBox.Show("程序已经运行了一个实例，该程序只允许有一个实例");
                    return;
                }

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                string serverErrorStr = "";
                int serverStatus = GetWindowsServiceStartType("NetTcpPortSharing", out serverErrorStr);
                if (serverStatus == 4)
                {
                    if (SetWindowsServiceStartType("NetTcpPortSharing", 2))
                    {

                    }
                    else
                    {
                        MessageBox.Show("无法更改服务NetTcpPortSharing的启动类型，请手动修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //

                        Environment.Exit(1);
                    }
                }
                Application.Run(new FrmTransferFileManager());
            }
            catch (Exception ex)
            {
                WriteAnalyseLog.WriteLogFile(DateTime.Now + "" + ex.Message);
            }
            
        }

        //public void InItJsonpara()
        //{
        //    var jsonParameters = new JSONParameters();
        //    jsonParameters.SerializeToLowerCaseNames = true;
        //    jsonParameters.EnableAnonymousTypes = true;
        //    jsonParameters.UseExtensions = false;
        //    jsonParameters.SerializeNullValues = false;
        //    jsonParameters.UsingGlobalTypes = false;
        //    jsonParameters.InlineCircularReferences = true;
        //    jsonParameters.UseEscapedUnicode = false;
        //}

        //取得服务的启动类型的当前状态，返回启动类型的状态値
        //2：自动，3：手动，4：禁用 ......
        //sServiceName服务名称
        //sState输出相关的状态代码，失败则为错误消息
        static int GetWindowsServiceStartType(String sServiceName, out String sState)
        {

           sState = "";

            try
            {
                System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo();

                objProcessInf.FileName = "cmd.exe";

                objProcessInf.UseShellExecute = false;

                objProcessInf.RedirectStandardError = true;
                objProcessInf.RedirectStandardInput = true;
                objProcessInf.RedirectStandardOutput = true;

                objProcessInf.CreateNoWindow = true;

                objProcessInf.Arguments = "/c sc qc " + sServiceName;

                System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objProcessInf);

                String sStateValue = objProcess.StandardOutput.ReadToEnd();

                if (sStateValue.IndexOf("AUTO_START") > 0)
                {
                    sState = "AUTO_START";
                    return 2;
                }

                if (sStateValue.IndexOf("DEMAND_START") > 0)
                {
                    sState = "DEMAND_START";
                    return 3;
                }

                if (sStateValue.IndexOf("DISABLED") > 0)
                {
                    sState = "DISABLED";
                    return 4;
                }

                return 0;

            }
            catch (Exception e)
            {
                sState = e.Message;
                return -1;
            }
        }
        //设置服务的启动类型
        //sServiceName服务名称
        //iStartType要设置的启动类型
        //返回成功状态 true：成功，false：失败
        static Boolean SetWindowsServiceStartType(String sServiceName, int iStartType)
        {
            try
            {
                System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo();

                objProcessInf.FileName = "cmd.exe";

                objProcessInf.CreateNoWindow = false;
                objProcessInf.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

                string sStartState = "boot";

                switch (iStartType)
                {
                    case 1:
                        {
                            sStartState = "system";
                            break;
                        }
                    case 2:
                        {
                            sStartState = "auto";
                            break;
                        }
                    case 3:
                        {
                            sStartState = "demand";
                            break;
                        }
                    case 4:
                        {
                            sStartState = "disabled";
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }

                objProcessInf.Arguments = "/c sc config " + sServiceName + " start= " + sStartState;

                System.Diagnostics.Process.Start(objProcessInf);

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
